<?php

namespace app\admin\controller\mental;

use app\common\controller\Backend;

/**
 * 
 */
class Config extends Backend
{

    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\mental\Config;
    }

    public function index()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a", [], 'trim');
            if ($params) {
                $configList = [];
                foreach ($this->model->all() as $v) {
                    if (isset($params[$v['name']])) {
                        $value = $params[$v['name']];
                        if (is_array($value) && isset($value['field'])) {
                            $value = json_encode($this->model::getArrayData($value), JSON_UNESCAPED_UNICODE);
                        } else {
                            $value = is_array($value) ? implode(',', $value) : $value;
                        }
                        $v['value'] = $value;                      
                        if(isset($params[$v['t_name']])){
                            $content = $params[$v['t_name']];
                            $v['content'] = $content;
                        }                        
                        $configList[] = $v->toArray();
                    }
                }
                $this->model->allowField(true)->saveAll($configList);
                $this->success();
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $data = $this->model->all();
        $this->assign('loupan_config', $data);
        return $this->view->fetch();
    }

    /**
     * 编辑
     */
    public function edit($ids = null)
    {
        $this->error(__('You have no permission'));
    }
    /**
     * 删除
     */
    public function del($ids = "")
    {
        $this->error(__('You have no permission'));
    }
    /**
     * 真实删除
     */
    public function destroy($ids = "")
    {
        $this->error(__('You have no permission'));
    }
    /**
     *添加
     */
    public function add()
    {
        $this->error(__('You have no permission'));
    }
}
